<?php
/**
 * Functions for ou-specific items on the resource page.
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 */

require_once(dirname(__FILE__).'/resourcepage_item.php');
require_once(dirname(__FILE__).'/extensions/etexts/etexts.php');
require_once(dirname(__FILE__).'/../../local/utils.php');
   

    /**
     * Displays the main content of a forum link
     */

   function display_forumlink($editmode,$editing,$courseid,$otheritem, &$rpi, $edit, $indeleted = false){

   	global $CFG;
   	
	$server = 1; 			// default to server 1
    $conference = '';
    $forumpathstudents = '';
    $forumpathstaff = '';
    $title = '';

    $conferences = array ('home' =>'Desktop', 'tutor' =>'Tutor group forum', 'specified' => 'Specific forum');

    $servers = array(1 => 'Server 1', 2 => 'Server 2');


           	// get the relevant fields
        	
        	$alltext = $rpi->get_field('weblink');
        	$title = $rpi->get_field('title');
        	$description = $rpi->get_field('description');
        	
        	// check to see whether all text has something in it before
        	// spliting the text
        	
        	if($alltext == '') {
        		
        		// do nothing
        		
        		
        	} else { // split the alltext field up
        	
        		$parray = explode(',', $alltext);

    			$server = $parray[0];
    			$conference = $parray[1];
    			$forumpathstudents = $parray[2];
    			$forumpathstaff = $parray[3];
    			
        	} // endif

        if(!$editing) {
        	        	
       		// need to create our own display stuff
        	
        	// need to create the url based on server type, etc
        	// look at code in mod/type/forumlink/resource.class.php

            $validforum = true;

			$url = '';
			$baseurl= $CFG->wwwroot.'/mod/resource/type/forumlink/fclink.php?id='.$courseid.'&conference='.$conference.'&server='.$server;
			
			if($conference == 'specified') {
				
				$url = $baseurl.'&forumpathstudents='.rawurlencode($forumpathstudents).'&forumpathstaff='.rawurlencode($forumpathstaff);

                $student = false;

                if(has_capability('moodle/legacy:teacher', get_context_instance(CONTEXT_COURSE, $courseid))) {

                    // do nothing

                } else {

                    // determine whether a user is a student

                    if (has_capability('moodle/legacy:student', get_context_instance(CONTEXT_COURSE, $courseid))) {

                        $student = true;

                    } else {

                        if(class_exists('SamsAuth') && SamsAuth::$inst->get_studentid()) {

                            $student = true;

                        } // endif

                    } // endif

                } // endif

                // test to see whether a valid forum

                if($student) {

                    // assume to be a student

                    if(empty($forumpathstudents)) {

                        // do not show

                       $validforum = false;

                    } else {

                        // do nothing

                    } // endif

                } else {  // assume to be a tutor

            	    if(empty($forumpathstaff)) {
                	
                	    // staff path is blank do not show


                       $validforum = false;
                	                                	               	
                    } else {

                        // do nothing

                    } // endif

                } // endif
					
			} else { 	
				
				$url = $baseurl;	
				
			} // endif

       		if(!$editmode) {

            	if($validforum) {

                	// display url etc
			         	
        	    	$rpi->display_header(
      				    $rpi->get_link_icon($url),true,
                 	    $url,
                 	    resourcepage_item::get_title_as_html($title),
                 	    $otheritem,
                 	    false, null, 'h3', false, $edit);
                
                // parameters " false, null, 'h3', false, $edit " added
                // to function call display header above
                
                 	   // display description
                 	    display_description($description);

            	} else { // invalid forum in display - so don't display

                	// do nothing

            	} // endif

          	} else {

            	// we are in edit mode so show
                // display url etc
			         	
        	    $rpi->display_header(
  				    $rpi->get_link_icon($url),true,
             	    $url,
             	    resourcepage_item::get_title_as_html($title),
             	    $otheritem,
             	    false, null, 'h3', false, $edit);
                
                // parameters " false, null, 'h3', false, $edit " added
                // to function call display header above              

             	    // display description
             	    display_description($description);

        	} // endif in edit mode

        } else { // in individual item editing mode
        	                	
            $rpi->print_editing_title('forumlink');
            $rpi->print_text_input('title', $title);
            $rpi->print_html_area('description',$description);
           	$rpi->print_dropdown('servers',$server, $servers);
           	print '<br>';
           	$rpi->print_dropdown('linkto',$conference, $conferences);        	       	
            $rpi->print_text_input('forumpathstudents',$forumpathstudents);
            $rpi->print_text_input('forumpathstaff',$forumpathstaff);

        }  // endif
   	  	   	  	
    }

    /**
     * Get combined desciption field for forum link
     */
   function get_forumlink_weblink() {

        // get server
        $server=required_param('servers',PARAM_RAW);

        // get conference link to
        $conference=required_param('linkto',PARAM_RAW);

        // get forum path students
        $forumpathstudents=required_param('forumpathstudents',PARAM_RAW);

        // get forum path staff
        $forumpathstaff=required_param('forumpathstaff',PARAM_RAW);

        // return combined description field
        return $server.','.$conference.','.$forumpathstudents.','.$forumpathstaff;
   }


   
   
   function display_description($description) {
        // If it's empty (or has nothing except tags), don't output it
        if(preg_replace('/\s|<.*?>/','',$description)!='') {
            // Disabling smilies
            $options = new stdClass;
            $options->smiley = false;
            print
                '<div class="rp_description">'.
                format_text($description, FORMAT_HTML, $options).
                '</div>';
        }
    }
   
/**
 * Updates sharing resource page presentation if
 * previous and current course codes match and
 * previous and sharing course presentations match to
 * sharing course code with current presentation
 * @param string $prevcourseshortname previous course shortname
 * @param string $currcourseshortname current course shortname
 * @param string $sharingcourseshortname sharing course shortname
 * @return string updated $sharingcourseshortname as relevant
 */
function update_sharing_shortname($prevcourseshortname, $currcourseshortname, $sharingcourseshortname) {

    if (($prevcodepres = get_course_code_pres($prevcourseshortname, true)) &&
        ($currcodepres = get_course_code_pres($currcourseshortname, true)) &&
        ($sharingcodepres = get_course_code_pres($sharingcourseshortname, true)) &&
        $prevcodepres[1] == $currcodepres[1] &&
        $prevcodepres[2] == $sharingcodepres[2]) {

        // Rolling course forward so return updated sharing course shortname
        $sharingcourseshortname = $sharingcodepres[1].'-'.$currcodepres[2];
    }
    return $sharingcourseshortname;
}

/**
 * Gets print on demand order printed material button.
 * Button is disabled if print on demand item is not
 * just a single recognisable book number/code.
 * @param string $title resourcepage file item title
 * @param array &$files reference to array of files for resourcepage file item
 * @param array $cm resourcepage file item course module object
 * @return string enabled or disabled print on demand button form
 */
function get_pod_info($title, &$files, $cm=NULL) {

    // Get print on demand button and associated info
    $podinfo = '';            

    // Bug 3116 - disable pod item if not just one recognisable book number/code
    $filename = '';
    $supcode = '';
    $disabled = ' disabled="disabled"';
    $files[0]->extrainfo = get_string('orderprintdisabled', 'resourcepage');
    for ($i = 0; $i < count($files); $i++) {
        // Bug 3076 - disable pod item if do not recognise book number/code
        $filename2 = basename($files[$i]->get_filepath());
        $matches = array();
        if (preg_match(resourcepage_etexts::MATCH_WEB, $filename2, $matches) ||
            preg_match(resourcepage_etexts::MATCH_ISBN, $filename2, $matches) ||
            preg_match(resourcepage_etexts::MATCH_SUP, $filename2, $matches)) {
            if (!$filename) {
                $filename = $filename2;
                $supcode = $matches[1];
                $disabled = '';
                $files[0]->extrainfo = '';
            } else if ($supcode != $matches[1]) {
                $disabled = ' disabled="disabled"';
                $files[0]->extrainfo = get_string('orderprintmismatch', 'resourcepage');
                break;
            }
        }
    }

    $id = empty($cm->id) ? 0 : $cm->id;
    $podinfo = ' <form method="post" action="order.php" class="rp_printondemand">'.
               '<input type="hidden" name="sesskey" value="'.sesskey().'" />'.
               '<input type="hidden" name="id" value="'.$id.'" />'.
//             '<input type="hidden" name="rpid" value="'.$this->parent->get_container().'" />'.
               '<input type="hidden" name="title" value="'.htmlspecialchars($title).'" />'.
               '<input type="hidden" name="filename" value="'.$filename.'" />'.
               '<input type="submit" name="addtoorder"'.$disabled.' value="Order print"/>'.
               '</form>';

    return $podinfo;
}

?>